﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using sexhealth.console.service.post.vo;
using sexhealth.console.service.post;

public partial class admin_UC01_0101indexpagepic : System.Web.UI.Page
{
    PostService postService = new PostService();
    //網站首頁圖片channel id
    int channelId = 12;
    protected void Page_Load(object sender, EventArgs e)
    {
        ShowPic();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {

    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        HttpFileCollection hfc = Request.Files;
        hfc[0].SaveAs(Server.MapPath("~\\") + "\\upload\\media\\" + System.IO.Path.GetFileName(hfc[0].FileName));
        IList<PostVO> postList = postService.GetPostList(channelId);
        if (postList.Count > 0)
        {
            postList[0].FileLink = System.IO.Path.GetFileName(hfc[0].FileName);
            postList[0].Title = "網站首頁圖片";
            postService.UpdatePost(postList[0]);
          
        }
        else
        {
            PostVO post = new PostVO();
            post.FileLink = System.IO.Path.GetFileName(hfc[0].FileName);
            post.BelongToChannelId = channelId;
            post.Title = "網站首頁圖片";
            postService.InsertPost(post);
        }
        ShowPic();
    }
    private void ShowPic()
    {
        IList<PostVO> postList = postService.GetPostList(channelId);
        if (postList.Count > 0)
        {
            ltlPic.Text = "<img src='../../upload/media/" + postList[0].FileLink + "'>";
        }
        else 
        {
            ltlPic.Text = "無圖片資料。";
        }
    }
}
